package com.xiaohai.big_event_system.utils;

import com.auth0.jwt.JWT;
import com.auth0.jwt.algorithms.Algorithm;

import java.util.Date;
import java.util.Map;

public class JwtUtil {
    private static final String secret = "xiaohai";
    private static final Integer expire = 1000 * 60 * 60;

    /**
     * 生成token
     *
     * @param claims
     * @return
     */
    public static String createToken(Map<String, Object> claims) {
        return JWT.create()
                .withClaim("claims", claims)// 添加载荷
                .withExpiresAt(new Date(System.currentTimeMillis() + expire))// 添加过期时间
                .sign(Algorithm.HMAC256(secret));// 指定密钥
    }

    /**
     * 解析token
     *
     * @param token
     * @return
     */
    public static Map<String, Object> parseToken(String token) {
        return JWT.require(Algorithm.HMAC256(secret))
                .build()
                .verify(token)
                .getClaim("claims")
                .asMap();
    }
}
